package cn.shyouchi.wuguanjia.android.ui.statistic;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.ui.attendance.statistics.StaffStatisticsActivity;
import cn.shyouchi.wuguanjia.android.utils.DisplayUtil;
import cn.shyouchi.wuguanjia.android.widget.GlideCircleTransform;

/**
 * Created by Eenie on 2017/1/22 at 13:13
 * Email: 472279981@qq.com
 * Des:
 */

public class StatisticGroupRefuelAdapter extends BaseMultiItemQuickAdapter<UserMonthRank, BaseViewHolder> {

    private String date;
    RankItemColorAgent mRankItemColorAgent;

    public StatisticGroupRefuelAdapter(List<UserMonthRank> data) {
        super(data);
        addItemType(StatisticGroupAdapter.STATISTIC_MONTH, R.layout.item_month_statistic_layout);
        addItemType(StatisticGroupAdapter.STATISTIC_YEAR, R.layout.item_group_year_statistic_layout);
        date = "";
        mRankItemColorAgent = new RankItemColorAgent();
    }

    @Override
    protected void convert(BaseViewHolder holder, final UserMonthRank entity) {


        switch (entity.getItemType()) {
            case StatisticGroupAdapter.STATISTIC_MONTH:

                holder.getView(R.id.img_rank).setVisibility(View.INVISIBLE);
                holder.getView(R.id.tv_rank).setVisibility(View.VISIBLE);
                holder.setText(R.id.tv_rank, String.valueOf(getData().size()-holder.getAdapterPosition() ));

                holder.setText(R.id.tv_user_name, String.format("%s | %s", entity.getName(), entity.getPermissions()));
                holder.setText(R.id.tv_user_day, String.format("出勤：%s天", entity.getWork_number()));
                holder.setText(R.id.tv_user_date, date);
                holder.setText(R.id.tv_rank_num, String.format("异常\n共%s次", entity.getRefue()));


                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(DisplayUtil.dip2px(mContext, 50), DisplayUtil.dip2px(mContext, 50));
                LinearLayout rankGroup = holder.getView(R.id.lin_rank_group);
                rankGroup.removeAllViews();

                if (!entity.getService().isEmpty()) {
                    for (UserMonthRank.ServiceBean sb : entity.getService()) {
                        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_statistic_item, null);
                        view.setLayoutParams(lp);
                        view.setBackgroundColor(mRankItemColorAgent.getColorByRankName(sb.getServicesname()));
                        TextView tvDes = (TextView) view.findViewById(R.id.tv_des_name);
                        TextView tvNum = (TextView) view.findViewById(R.id.tv_des_num);
                        tvDes.setText(sb.getServicesname());
                        tvNum.setText(String.valueOf(sb.getWork_day()));
                        rankGroup.addView(view);
                    }
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, StaffStatisticsActivity.class);
                        intent.putExtra("userid", entity);
                        intent.putExtra("date", date);
                        mContext.startActivity(intent);
                    }
                });
                break;

            case StatisticGroupAdapter.STATISTIC_YEAR:


                holder.getView(R.id.img_rank).setVisibility(View.INVISIBLE);
                holder.getView(R.id.tv_rank_position).setVisibility(View.VISIBLE);
                holder.setText(R.id.tv_rank_position, String.valueOf(holder.getAdapterPosition() + 1));

                holder.setText(R.id.tv_name, String.format("%s %s", entity.getName(), entity.getPermissions()));
                holder.setText(R.id.tv_rank_num, String.format("异常次数：%s次", entity.getRefue()));
                ImageView img = holder.getView(R.id.img_header);
                loadHead(Global.BASE_URL + entity.getId_card_head_image(), img);
                break;
        }
    }


    private void loadHead(String path, ImageView imageView) {
        Glide.with(mContext)
                .load(path)
                .placeholder(R.mipmap.ic_default_header)
                .transform(new GlideCircleTransform(mContext))
                .into(imageView);
    }

    public void setDate(String date) {
        this.date = date;
    }
}
